﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Comdiv.QWeb.Factory;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.Binding {
    [TestFixture]
    public class default_binder_test {
        public class intbind : IAction {
            [Bind] private int id;
            [Bind(5)] private int id2;

            public object Process(QWebContext context) {
                if (0 != id) return id;
                return id2;
            }
        }

        public class boolbind : IAction {
            [Bind] private bool yes;

            public object Process(QWebContext context) {
                return yes;
            }
        }

        public class boolbind2 : IAction {
            [Bind(true)] private bool yes;

            public object Process(QWebContext context) {
                return yes;
            }
        }


        private IQWebFactory f;
        private QWebServiceRegistry r;

        [SetUp]
        public void setup() {
            this.r = new QWebServiceRegistry();
            this.f = r.Factory;
            f.RegisterAction<intbind>("test.int");
            f.RegisterAction<boolbind>("test.bool");
            f.RegisterAction<boolbind2>("test.bool2");
        }

        public object getResult(string name, IDictionary<string, string> parameters) {
            var ctx = new QWebContext {Registry = r};
            ctx.ActionName = name;

            if (null != parameters) {
                foreach (var parameter in parameters) {
                    ctx.Parameters[parameter.Key] = parameter.Value;
                }
            }
            ctx.Setup();
            return ctx.Process();
        }

        [Test]
        public void simple_int_test() {
            Assert.AreEqual(2, getResult("test.int", new Dictionary<string, string> {{"id", "2"}}));
        }

        [Test]
        public void default_int_test() {
            Assert.AreEqual(5, getResult("test.int", null));
            Assert.AreEqual(10, getResult("test.int", new Dictionary<string, string> {{"id2", "10"}}));
        }

        [Test]
        public void ws_int_test() {
            Assert.AreEqual(2, getResult("test.int", new Dictionary<string, string> {{"id", " 2 "}}));
        }

        [Test]
        public void simple_bool_test() {
            Assert.AreEqual(true, getResult("test.bool", new Dictionary<string, string> {{"yes", "True"}}));
        }

        [Test]
        public void default_bool_test() {
            Assert.AreEqual(true, getResult("test.bool2", new Dictionary<string, string> {{"yes", "True"}}));
            Assert.AreEqual(false, getResult("test.bool2", new Dictionary<string, string> {{"yes", "False"}}));
            Assert.AreEqual(true, getResult("test.bool2", null));
        }

        [Test]
        public void case_bool_test() {
            Assert.AreEqual(true, getResult("test.bool", new Dictionary<string, string> {{"yes", "true"}}));
        }

        [Test]
        public void ws_bool_test() {
            Assert.AreEqual(true, getResult("test.bool", new Dictionary<string, string> {{"yes", " true "}}));
        }

        [Test]
        public void num_bool_test() {
            Assert.AreEqual(true, getResult("test.bool", new Dictionary<string, string> {{"yes", "1"}}));
        }
    }
}